package com.ecoolex.ying.space.core.manager;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.common.util.CollectorUtil;
import com.ecoolex.ying.space.core.dao.GoodsSpecDAO;
import com.ecoolex.ying.space.entity.GoodsSpec;
import com.ecoolex.ying.space.entity.GoodsSpec.Column;
import com.ecoolex.ying.space.entity.GoodsSpecExample;
import com.ecoolex.ying.space.entity.GoodsSpecGroup;
import com.ecoolex.ying.space.pojo.EditSpecDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.time.Instant;
import java.util.List;

@Service
public class GoodsSpecManager {
    @Autowired
    private GoodsSpecDAO dao;

    private void verify(EditSpecDTO input) {
        BizAssert.isTrue(Check.notNullOrTrimEmpty(input.getName()), ResultCode.VERIFY.build(41), "规格名称为空");
    }

    void update(GoodsSpecGroup group, final List<EditSpecDTO> specList, String updateUser) {
        List<GoodsSpec> oldList = queryByGroupId(group.getSpecGroupId());

        for (int i = 0, size = specList.size(); i < size; i++) {
            EditSpecDTO input = specList.get(i);
            if (Check.notNull(input.getSpecId())) {
                GoodsSpec old
                        = oldList.stream().collect(CollectorUtil.first(s -> input.getSpecId().equals(s.getSpecId())));
                BizAssert.isTrue(Check.notNull(
                        old), ResultCode.VERIFY.build(40), "规格分组={}下规格值={}不存在", group.getName(), input.getSpecId());

                old.setName(input.getName());
                old.setSorting(i + 1);
                update(old, updateUser);
                // 移除旧的数组
                oldList.remove(old);
            } else {
                GoodsSpec spec = new GoodsSpec();
                spec.setGoodsId(group.getGoodsId());
                spec.setSpecGroupId(group.getSpecGroupId());
                spec.setName(input.getName());
                spec.setSorting(i + 1);
                create(spec, updateUser);
                // 设置新增规格值的id,以备规格时使用
                input.setSpecId(spec.getSpecId());
            }
        }

        oldList.forEach(old -> delete(old, updateUser));
    }

    private void update(GoodsSpec record, String updateUser) {
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.updateByPrimaryKeySelective(record, Column.name, Column.sorting, Column.updateUser, Column.updateTime);
    }

    private void create(GoodsSpec record, String createUser) {
        record.setDeleted(GoodsSpec.NOT_DELETED);
        record.setCreateUser(createUser);
        record.setCreateTime(Date.from(Instant.now()));
        record.setUpdateUser(createUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.insert(record);
    }

    private void delete(GoodsSpec record, String updateUser) {
        record.setDeleted(GoodsSpec.IS_DELETED);
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.updateByPrimaryKeySelective(record, Column.deleted, Column.updateUser, Column.updateTime);
    }

    void deleteByGroupId(Integer groupId, String updateUser) {
        GoodsSpecExample example = new GoodsSpecExample();
        example.createCriteria().andSpecGroupIdEqualTo(groupId).andDeletedEqualTo(GoodsSpec.NOT_DELETED);

        GoodsSpec record = new GoodsSpec();
        record.setDeleted(GoodsSpec.IS_DELETED);
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));

        dao.updateByExampleSelective(record, example, Column.deleted, Column.updateUser, Column.updateTime);
    }

    void deleteByGoodsId(Integer goodsId, String updateUser) {
        GoodsSpecExample example = new GoodsSpecExample();
        example.createCriteria().andGoodsIdEqualTo(goodsId).andDeletedEqualTo(GoodsSpec.NOT_DELETED);

        GoodsSpec record = new GoodsSpec();
        record.setDeleted(GoodsSpec.IS_DELETED);
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));

        dao.updateByExampleSelective(record, example, Column.deleted, Column.updateUser, Column.updateTime);
    }

    public List<GoodsSpec> queryByGroupId(Integer groupId) {
        GoodsSpecExample example = new GoodsSpecExample();
        example.createCriteria().andSpecGroupIdEqualTo(groupId).andDeletedEqualTo(GoodsSpec.NOT_DELETED);
        example.orderBy(Column.sorting.asc());
        return dao.selectByExample(example);
    }


}